package com.yangfei.mystudy1020.View;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;

import com.yangfei.mystudy1020.R;

/**
 * TODO: document your custom view class.
 */
public class MaskView extends AppCompatImageView {

    public static final int BatMode_Normal = 1;
    public static final int BatMode_Disconnect = 2;
    public static final int BatMode_Charging = 3;
    private String tag = "未连接相机";

    private int mImgMask;
    private int mColor;
    private int percent;
    private int mViewMode;
    private int batTextSize;

    private Bitmap mask;
    private Bitmap result;
    private Canvas mCanvas;

    private Paint paint;
    private Paint paintBack = new Paint();
    private Paint paintRect = new Paint();
    private Paint paintText = new Paint();

    RuntimeException mException;
    PorterDuffXfermode mMode;

    Rect tagBounds;

    Context mCtx;

    Rect sd = new Rect(0, 0, 0, 0);

    public MaskView(Context context) {
        super(context);
        mCtx = context;
        init(null, 0);
    }

    public MaskView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mCtx = context;
        init(attrs, 0);
    }

    public MaskView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mCtx = context;
        init(attrs, defStyle);
    }

    private void init(AttributeSet attrs, int defStyle) {
        // Load attributes
        final TypedArray a = getContext().obtainStyledAttributes(
                attrs, R.styleable.MaskView, defStyle, 0);

        percent = a.getInt(R.styleable.MaskView_batPercent, 0);
        mColor = a.getColor(R.styleable.MaskView_imgColor, 0);
        mViewMode = a.getInt(R.styleable.MaskView_batMode, -1);
        batTextSize = a.getInt(R.styleable.MaskView_batTextSize, -1);

        mImgMask = a.getResourceId(R.styleable.MaskView_imgMask, 0);
        if (mImgMask == 0) {
            mException = new IllegalArgumentException(a.getPositionDescription() +
                    ": The content attribute is required and must refer to a valid image.");
        }

        a.recycle();

        //获取遮罩层图片
        mask = BitmapFactory.decodeResource(getResources(), mImgMask);

        result = Bitmap.createBitmap(mask.getWidth(), mask.getHeight(), Bitmap.Config.ARGB_8888);

        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mMode = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);

        tagBounds = new Rect();
        paintText.setColor(0xff0e0e0e);
        paintText.setTextSize(sp2px(mCtx, batTextSize));
        paintText.setTextAlign(Paint.Align.LEFT);

        paintText.getTextBounds(tag, 0, tag.length(), tagBounds);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        switch (mViewMode) {
            case BatMode_Normal: {
                drawNormal(canvas);
                break;
            }
            case BatMode_Disconnect: {
                drawDisconnect(canvas);
                break;
            }
            case BatMode_Charging: {
                drawCharging(canvas);
                break;
            }
            default: {
                break;
            }
        }

    }

    private void drawNormal(Canvas canvas) {
        //底图
        mCanvas.drawBitmap(mask, 0, 0, paintBack);
        //色块进度条
        int ip = (int) (mask.getWidth() * (percent / 100.f));
        sd.set(0, 0, ip, mask.getHeight());
        paint.setXfermode(mMode);
        paintRect.setColor(mColor);
        mCanvas.drawRect(sd, paintRect);
        mCanvas.drawBitmap(mask, 0, 0, paint);
        paint.setXfermode(null);
        setImageBitmap(result);
        setScaleType(ImageView.ScaleType.CENTER);
    }

    private void drawDisconnect(Canvas canvas) {
        canvas.drawText(tag, getMeasuredWidth() - tagBounds.width(), tagBounds.height(), paintText);
        //底图drawText
        canvas.drawBitmap(mask, getMeasuredWidth() - mask.getWidth(), tagBounds.height(), paintBack);
    }

    private void drawCharging(Canvas canvas) {

    }

    public void setPercent(int p) {
        percent = p;
        invalidate();
    }

    public void setmColor(int c) {
        mColor = c;
        invalidate();
    }

    public static int sp2px(Context context, float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        final int minimumWidth = getSuggestedMinimumWidth();
        final int minimumHeight = getSuggestedMinimumHeight();
        Log.e("YView", "---minimumWidth = " + minimumWidth + "");
        Log.e("YView", "---minimumHeight = " + minimumHeight + "");
        int width = measureWidth(minimumWidth, widthMeasureSpec);
        int height = measureHeight(minimumHeight, heightMeasureSpec);
        setMeasuredDimension(width, height);
    }

    private int measureWidth(int defaultWidth, int measureSpec) {

        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        Log.e("YViewWidth", "---speSize = " + specSize + "");


        switch (specMode) {
            case MeasureSpec.AT_MOST:
                Log.e("YViewWidth", "---speMode = AT_MOST");
                if(tagBounds.width() > mask.getWidth()){
                    defaultWidth = tagBounds.width();
                }else{
                    defaultWidth = mask.getWidth();
                }
                break;
            case MeasureSpec.EXACTLY:
                Log.e("YViewWidth", "---speMode = EXACTLY");
                defaultWidth = specSize;
                break;
            case MeasureSpec.UNSPECIFIED:
                Log.e("YViewWidth", "---speMode = UNSPECIFIED");
                defaultWidth = Math.max(defaultWidth, specSize);
        }
        return defaultWidth;
    }


    private int measureHeight(int defaultHeight, int measureSpec) {

        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        Log.e("YViewHeight", "---speSize = " + specSize + "");

        switch (specMode) {
            case MeasureSpec.AT_MOST:
                defaultHeight = tagBounds.height()+mask.getHeight()+2;
                Log.e("YViewHeight", "---speMode = AT_MOST");
                break;
            case MeasureSpec.EXACTLY:
                defaultHeight = specSize;
                Log.e("YViewHeight", "---speSize = EXACTLY");
                break;
            case MeasureSpec.UNSPECIFIED:
                defaultHeight = Math.max(defaultHeight, specSize);
                Log.e("YViewHeight", "---speSize = UNSPECIFIED");
                break;
        }
        return defaultHeight;


    }

}
